const iconv = require('iconv-lite');
const {execSync, spawnSync} = require('child_process');
 
/**
 * 同步执行命令
 */
function execSyncS(cmd, args, cmdOpation={}) {
    console.log("cmd: ", cmd);
    console.log("args: ", args);
    // const encoding = 'cp936';
    const encoding = 'utf8';
    const binaryEncoding = 'binary';
    const t = spawnSync(cmd, args, { encoding: binaryEncoding, shell: true, ...cmdOpation});
    const consoleM = iconv.decode(Buffer.from(t.stdout, binaryEncoding), encoding);
    console.log("consoleM: ", consoleM);
    console.log("consoleM Error: ", iconv.decode(Buffer.from(t.stderr, binaryEncoding), encoding));
    return consoleM;
}

function execSyncGit(cmd, args, cmdOpation={}) {
    cmd = JSON.parse(JSON.stringify(cmd));
    args = cmd.splice(1, cmd.length).concat(args)
    return execSyncS(cmd[0], args, cmdOpation);
}

module.exports = {execSyncS, execSyncGit};